package spotGUI.utils;

import java.awt.Component;

import javax.swing.DefaultListCellRenderer;
import javax.swing.ImageIcon;
import javax.swing.JLabel;
import javax.swing.JList;
import javax.swing.ListCellRenderer;
import javax.swing.UIManager;

import managers.messengerManager.MessengerManager;
import utils.UserData;
import utils.UserState;


public class UserDataListCellRenderer implements ListCellRenderer {

	private DefaultListCellRenderer defaultRenderer = new DefaultListCellRenderer();


	@Override
	public Component getListCellRendererComponent(JList list, Object value,
			int index, boolean isSelected, boolean cellHasFocus) {

		JLabel listCellRendererComponent = (JLabel) defaultRenderer.getListCellRendererComponent(list, value, index, isSelected, cellHasFocus);
		UserData userData = (UserData)value;

		listCellRendererComponent.setBackground(UIManager.getColor("textHighlight"));
		
		listCellRendererComponent.setText(userData.getProperty("firstName")+" "+userData.getProperty("surname"));
		if ((Boolean) userData.getProperty("online")) {
			listCellRendererComponent.setIcon(new ImageIcon(MessengerManager.getSharedInstance().getFeedbackManager().getIconPath((UserState)userData.getProperty("state"))));
			listCellRendererComponent.setToolTipText(((UserState)userData.getProperty("state")).getText());
		}
		else {
			listCellRendererComponent.setIcon(new ImageIcon(UserState.class.getResource("/images/Icons/24x24px/delete.png")));
			listCellRendererComponent.setToolTipText("OFFLINE");
		}
		

		
		Boolean matching = (Boolean) userData.getProperty("matchingWithSearchKey");
		if (isSelected || (matching != null && matching)) {
			listCellRendererComponent.setOpaque(true);
		} else {
			listCellRendererComponent.setOpaque(false);
		}
		
		return listCellRendererComponent;

	}

}
